﻿

using MicroCenter.Models;
using MicroCenter.Services;
using MicroCenter.Tokens;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

using Newtonsoft.Json.Linq;

using System;
using System.Linq;
using System.Reflection;

namespace MicroCenter.Controllers
{
    [Auth]
    [ApiController]
    [Route("api/[controller]")]
    public class MicroController : ControllerBase
    {
        public readonly MicroService microService;

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="passwordDTO"></param>
        [Auth(false), Post]
        public string ChangePassword([FromBody] ChangePasswordDTO passwordDTO)
        {
            microService.UpdatePassWord(passwordDTO);
            return this.CreartTicket(new AdminToken() { Password = passwordDTO.Password });
        }


        /// <summary>
        /// 是否拥有初始账号
        /// </summary>
        [Auth(false), Get]
        public bool IsInit() => microService.IsInit();


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginDTO"></param>
        /// <returns></returns>
        [Auth(false), Post]
        public string Login([FromBody] LoginDTO loginDTO)
        {
            string password = microService.Login(loginDTO);
            return this.CreartTicket(new AdminToken() { Password = password });
        }

        /// <summary>
        /// 获取已注册的服务
        /// </summary>
        /// <returns></returns>
        [Get("getRegistries")]
        public RegistryModel[] GetRegistries() => microService.GetRegistries();

    }
}
